(function () {
    console.log("src\bbs_xiaomi\main.js is runnig!!!");
    const store = new Map();
    var Monitor = {
        addTabId:function(tabId){
            var expire = store.get(tabId);
            var now = Date.now();
            if(expire && (now - expire) < 5000){
                return false;
            }
            store.set(tabId, now);
            return true;
        },
        init: function () {
            chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
                
                if (Monitor.addTabId(tabId) && tab.url.indexOf('http://bbs.xiaomi.cn/t-') != -1) {
                    Monitor.reply(tabId);
                }
            })
        },
        reply: function (tabId) {
            var scriptCode = `(function(){
                setTimeout(() => {
                    autoReply();
                }, 1000);
                var autoReply = function(){
                    __replytxt = document.getElementById('J_scollReplytxt');
                    __reply = document.getElementById('J_scollReply');
                    
                    __replytxt.setAttribute("value", "hello_world");
                    __reply.click()
                    console.log(__replytxt);
                    console.log(__reply);
                }
            })()   
            `;
            chrome.tabs.executeScript(tabId, {
                file: 'src/bbs_xiaomi/execute_script.js',
            }, function (arrayResult) {
                console.log('execute script success!!!');
            })
        }
    }

    Monitor.init();
})()